package com.wzy.media.api;

import com.wzy.base.model.RestResponse;
import com.wzy.media.model.dto.UploadFileParamsDto;
import com.wzy.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @Author wzy
 * @Date 2024/1/13 17:21
 * @description: 大文件上传接口（视频）
 */
@Api(value = "大文件上传接口", tags = "大文件上传接口")
@RestController
public class BigFilesController {
    @Autowired
    private MediaFileService mediaFileService;
    @ApiOperation("文件上传前检查文件是否上传过")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkFile(@RequestParam("fileMd5") String fileMd5) throws Exception{

        return  mediaFileService.checkFile(fileMd5);
    }
    @ApiOperation("分块文件上传前检测")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkChunk(@RequestParam("fileMd5") String fileMd5,@RequestParam("chunk") int chunk) throws Exception{
        return mediaFileService.checkChunk(fileMd5,chunk);
    }
    @ApiOperation("上传分块文件")
    @PostMapping("/upload/uploadchunk")
    public RestResponse<Boolean> uploadChunk(@RequestParam("file") MultipartFile file,
                                             @RequestParam("fileMd5") String fileMd5,
                                             @RequestParam("chunk") int chunk) throws Exception {
        //创建一个临时文件
        File tempFile = File.createTempFile("minio", ".temp");
        //上传的文件拷贝到临时文件
        file.transferTo(tempFile);
        //文件路径
        String localFilePath = tempFile.getAbsolutePath();
        return mediaFileService.uploadChunk(fileMd5,chunk,localFilePath);    }
    @ApiOperation("合并文件")
    @PostMapping("upload/mergechunks")
    public RestResponse<Boolean> mergeChunks(@RequestParam("fileMd5") String fileMd5,
                                             @RequestParam("fileName") String fileName,
                                             @RequestParam("chunkTotal") int chunkTotal) throws Exception{
        Long companyId = 1232141425L;
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFilename(fileName);
        uploadFileParamsDto.setFileType("001002");
        uploadFileParamsDto.setTags("课程视频");
        uploadFileParamsDto.setRemark("");
        return mediaFileService.mergeChunks(companyId,fileMd5,chunkTotal,uploadFileParamsDto);
    }
}
